package com.tadpole.music.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.tadpole.music.R;

import java.util.List;

/**
 * 行业弹窗
 * Created by SunYan
 * on 2020/4/26.
 */
public class GenderDialog extends Dialog {

    private TextView tv_ok;
    private TextView tv_exit;
    private SmartWheelView smartWheelView;
    private IModeSelection iModeSelection ;
    private String mode;
    private List<String> datas;
    private TextView tvTitle;
    private String title;

    public GenderDialog(@NonNull Context context) {
        super(context);
    }

    public GenderDialog(@NonNull Context context, int themeResId, List<String> datas, String title) {
        super(context,themeResId);
        this.datas = datas;
        this.title = title;
    }

    protected GenderDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    /**
     * 初始化控件
     */
    public void initView(IModeSelection iModeSelection ) {
        this.iModeSelection = iModeSelection;
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(getOwnerActivity().LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.gender_dialog,
                null);
        tv_ok = view.findViewById(R.id.tv_ok);
        tv_exit = view.findViewById(R.id.tv_exit);
        tvTitle  = view.findViewById(R.id.tvTitle);
        smartWheelView = view. findViewById(R.id.smartWheelView);
        tvTitle.setText(title);
        smartWheelView.setDatas(datas);
        smartWheelView.setSeletion(0);
        mode=datas.get(0);

        viewListener();
        setContentView(view);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);

    }

    public void setModeListener(IModeSelection iModeSelection) {
        this.iModeSelection = iModeSelection;
    }

    @Override
    public void show() {
        super.show();
    }

    private void viewListener() {
        tv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        smartWheelView.setOnWheelSelectedListener(new SmartWheelView.OnWheelSelectedListener() {
            @Override
            public void onSelected(int selectedIndex, String mode1) {
                mode = mode1;
            }
        });

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iModeSelection.getMode(mode);
                dismiss();
            }
        });
    }

    public interface IModeSelection{
        void getMode(String mode);
    }
}
